package com.wfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wfx.entity.*;
import com.wfx.mapper.CmsContentCatMapper;
import com.wfx.service.ICmsContentCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 内容分类 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2021-01-23
 */
@Service
public class CmsContentCatServiceImpl extends ServiceImpl<CmsContentCatMapper, CmsContentCat> implements ICmsContentCatService {

    @Autowired
    private  CmsContentCatMapper cmsContentCatMapper;

    @Override
    public PageResult<CmsContentCat> search(PageDTO<CmsContentCat> pageDTO) {
        IPage<CmsContentCat> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        QueryWrapper queryWrapper = new QueryWrapper(pageDTO.getData());
        if (!StringUtils.isEmpty(pageDTO.getData().getName())){
            queryWrapper.eq("name",pageDTO.getData().getName());
        }
        IPage iPageInfo = cmsContentCatMapper.selectPage(iPage, queryWrapper);
        PageResult<CmsContentCat> pageResult = new PageResult<>();
        pageResult.setMsg("甲方妈妈，别搞我了");
        pageResult.setSuccess(true);
        pageResult.setPages(iPageInfo.getPages());
        pageResult.setTotal(iPageInfo.getTotal());
        pageResult.setData(iPageInfo.getRecords());

        return pageResult;
    }

    @Override
    public Result upAndAdd(CmsContentCat cmsContentCat) {
        if(cmsContentCat.getId()==null){  //判断id是否存在 ，为空就是添加
            int i = cmsContentCatMapper.insert(cmsContentCat);
            return new Result(i>0,"添加成功");

        }else { //否则就是修改
            int i = cmsContentCatMapper.updateById(cmsContentCat);
            return new Result(i>0,"添加成功");

        }
    }

    @Override
    public Result del(List<String> ids) {
        int i = cmsContentCatMapper.deleteBatchIds(ids);
        return new Result(i>0?true:false,i>0?"删除成功":"删除失败");
    }
    //所有分类
    @Override
    public Result getAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<CmsContentCat> cmsContentCats = cmsContentCatMapper.selectList(queryWrapper);
        return new Result(true,"小机灵鬼",cmsContentCats);
    }
}
